﻿namespace ShellNavigator.Client
{
  #region

  using System;
  using System.Collections;
  using System.Collections.Generic;
  using System.Linq;
  using System.Windows;

  using ShellNavigator.Client.Core;
  using ShellNavigator.Client.Models;
  using ShellNavigator.Client.Utils;

  #endregion

  /// <summary>
  ///     Interaction logic for App.xaml
  /// </summary>
  public partial class App : Application
  {
    #region Methods

    private static void ExecuteOnError(Exception ex)
    {
      MainWindowModel.ExecuteOnError(ex);
      Current.Shutdown();
    }

    private static void ExecuteOnSuccess(Request request, IEnumerable<InfoBase> results)
    {
      if (results != null && results.Count() == 1)
      {
        results.Single().RunAction(request.Action);
        Current.Shutdown();
      }
      else
      {
        var mainWindow = new MainWindow { DataContext = new MainWindowModel(request, results) };
        mainWindow.Show();
      }
    }

    private static void ProcessArgs(string[] args)
    {
      switch (args.Length)
      {
        case 1: // ShellNavigator.exe filemask
          {
            var request = new Request
            {
              Action = AppActions.OpenSolution,
              Target = AppTargets.Solution,
              Directory = string.Empty,
              Filemask = args[0],
              Text = string.Empty,
            };

            SearchEngine.VisualSearch(request, ExecuteOnSuccess, ExecuteOnError);
          }

          break;
        case 2: // ShellNavigator.exe action filemask
          {
            var request = new Request
            {
              Action = ParseAction(args[0]),
              Target = ParseTarget(args[0]),
              Directory = string.Empty,
              Filemask = args[1],
              Text = string.Empty,
            };
            SearchEngine.VisualSearch(request, ExecuteOnSuccess, ExecuteOnError);
          }

          break;
        case 3: // ShellNavigator.exe action directory filemask
          {
            var request = new Request
            {
              Action = ParseAction(args[0]),
              Target = ParseTarget(args[0]),
              Directory = args[1],
              Filemask = args[2],
              Text = string.Empty,
            };
            SearchEngine.VisualSearch(request, ExecuteOnSuccess, ExecuteOnError);
          }

          break;
        case 4: // ShellNavigator.exe action directory filemask regex
          {
            var request = new Request
            {
              Action = ParseAction(args[0]),
              Target = ParseTarget(args[0]),
              Directory = args[1],
              Filemask = args[2],
              Text = args[3],
            };
            SearchEngine.VisualSearch(request, ExecuteOnSuccess, ExecuteOnError);
          }

          break;
      }
    }

    private static AppTargets ParseTarget(string arg)
    {
      var action = ParseAction(arg);
      var target = action.ToTarget();
      return target;
    }

    private static AppActions ParseAction(string arg)
    {
      AppActions action;
      Enum.TryParse(arg, true, out action);
      return action;
    }

    private void AppStartup(object sender, StartupEventArgs e)
    {
      var args = e.Args;

      ////Debugger.Launch();
      if (!args.Any())
      {
        var mainWindow = new MainWindow { DataContext = new MainWindowModel() };
        mainWindow.Show();
      }
      else if (args.Any(argument => argument.EqualsAny("?", "/?", "/help")))
      {
        new HelpWindow().Show();
      }
      else
      {
        ProcessArgs(args);
      }
    }

    #endregion
  }
}